package com.AdvancedDemos.LearnVariousAPI.时间日期类.JDK8新时间日期类;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;

public class LearnPeriod_Duration {
    public static void main(String[] args) {
        /**
         * Period 用于计算两个 LocalDate对象 相差的年数,月数,天数 , 无法计算LocalTime对象相差的时分秒毫秒纳秒,要使用Duration解决
         *
         * public static Period between(LocalDate start,LocalDate end) --- 传入2个日期对象,得到Period对象
         * public int getYears() --- 计算间隔几年,并返回
         * public int getMonths() --- 计算间隔几个月,并返回
         * public int getDays() --- 计算间隔多少天,并返回
         */

        LocalDate ld1 = LocalDate.of(2029, 8, 10);
        LocalDate ld2 = LocalDate.of(2029, 12, 15);
        Period between = Period.between(ld1, ld2);//第一个参数是开始时间,第二个参数是结束时间
        System.out.println(between.getYears());
        System.out.println(between.getMonths());
        System.out.println(between.getDays());


        /**
         * Duration 代表持续时间,可以用于计算两个时间对象相差的天数,小时数,分数,秒数,纳秒数,支持LocalTime,LocalDateTime,Instant等时间
         *
         * public static Duration between(开始时间对象1,截止时间对象2) --- 传入2个时间对象,得到Duration对象
         * public long toDays() --- 计算间隔多少天,并返回
         * public long toHours(0 --- 计算间隔多少小时,并返回
         * public long toMinutes() --- 计算间隔多少分钟,并返回
         * public long toSeconds() --- 计算间隔多少秒,并返回
         * public long toMillis() --- 计算间隔多少毫秒,并返回
         * public long toNanos() --- 计算间隔多少纳秒,并返回
         */

        System.out.println("-------------");
        LocalDateTime start = LocalDateTime.of(2025, 11, 11, 11, 10, 10);
        LocalDateTime end = LocalDateTime.of(2025, 11, 11, 11, 11, 11);
        Duration duration = Duration.between(start, end);
        System.out.println(duration.toDays());
        System.out.println(duration.toHours());
        System.out.println(duration.toMinutes());
        System.out.println(duration.toSeconds());
        System.out.println(duration.toMillis());
        System.out.println(duration.toNanos());


    }
}
